package xdpm.nhom16.GUI;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

import xdpm.nhom16.R;
import xdpm.nhom16.BUS.SearchEngine;
import xdpm.nhom16.helper.Utilities;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class Autocomplete {

	private AutoCompleteTextView txtHomeSearch;
	private SearchEngine engine;
	private Activity activity;
//	private static ArrayList<String> listTenDichVu = new ArrayList<String>();
//	private static ArrayList<String> listTenDuong = new ArrayList<String>();
//	private ArrayList<String> listDichVuDuong = new ArrayList<String>();
//	private ArrayList<String> listDuong = new ArrayList<String>();
	
	private String str;
	
	private static int temp = 0;

	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str = str;
	}
	

	public Autocomplete(AutoCompleteTextView textView, SearchEngine engine, Activity activity){
		this.txtHomeSearch = textView;
		this.activity = activity;
		this.engine = engine;
	}
	
	public void test(){
		
	//	listTenDichVu = engine.getTenDichVu();
				
	/*	final String [] dichVuSearcher = new String[listTenDichVu.size()];
		
		for (int i = 0; i < dichVuSearcher.length; i++){
			dichVuSearcher[i] = listTenDichVu.get(i);
		//	System.out.println("data: " + dichVuSearcher[i]);
		}*/
		
	//	listTenDuong = engine.getTenDuong();
		
	/*	final String [] duongSearcher = new String[listTenDuong.size()];
		
		for (int i = 0; i < duongSearcher.length; i++){
			duongSearcher[i] = listTenDuong.get(i);
		//	System.out.println("data: " + duongSearcher[i]);
		}*/
		
	//	final int length = listTenDuong.size() * listTenDichVu.size();


	/*	for (int i = 0; i < activity.listTenDichVu.size(); i++){
			listDichVuDuong.add(Utilities.formatInputData(activity.listTenDichVu.get(i)));
		}
		
		
		//	final String [] searcher = new String[length];
			
			for (int i = 0; i < activity.listTenDuong.size(); i++){
				listDuong.add(Utilities.formatInputData(activity.listTenDuong.get(i)));
			}*/
		
	/*	for (int j = 0; j < listTenDuong.size(); j++) {
			listDichVuDuong.add(Utilities.formatInputData(duongSearcher[j]));
		}*/
		
/*		int counter = 0;
		for (int i = 0; i < listTenDichVu.size(); i++) {
			for (int j = 0; j < listTenDuong.size(); j++) {
				listDichVuDuong.add(Utilities.formatInputData(dichVuSearcher[i])
						+ " " + Utilities.formatInputData(duongSearcher[j]));
			
		//		System.out.println("Size: " + listDichVuDuong.get(counter++));
			}
		}*/
		
		
		final ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();

		Random rand = new Random();
		
		for (int i = 0; i < DichVuHCMActivity.listDichVuDuong.size(); i++) {
			ArrayList<String> listDichVu = new ArrayList<String>();
			int value = rand.nextInt(DichVuHCMActivity.listDuong.size() - 3);
			for (int j = value; j < value + 3; j++) {
				listDichVu.add(DichVuHCMActivity.listDichVuDuong.get(i)
						+ " " + DichVuHCMActivity.listDuong.get(j));
			}
			list.add(listDichVu);
		}
		
	//	final ArrayAdapter<String> adapter = new ArrayAdapter<String>(DichVuHCMActivity.this, R.layout.list_item, listDichVuDuong);
		    
			
	//	System.out.println("Size: " + adapter.getCount());
		    
		/* Chia nhỏ dữ liệu ra */	
	//	txtHomeSearch.setAdapter(adapter);
		
	//	txtHomeSearch.setTokenizer(new SpaceTokenizer());
	
		TextWatcher textWatcher = new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				textChanged(DichVuHCMActivity.listDichVuDuong, list, DichVuHCMActivity.listDuong);
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		};
		
		
		txtHomeSearch.addTextChangedListener(textWatcher);
		
		
		System.out.println("str: " + getStr());
	}
	
	
	public void textChanged(ArrayList<String> listDichVuDuong, ArrayList<ArrayList<String>> list, ArrayList<String> listDuong){
		setStr(txtHomeSearch.getText().toString().trim());
		System.out.println("seach: " + getStr());
		
		
		
		int i = searchItem(getStr(), listDichVuDuong);
		System.out.println("seach: " + getStr() + " - " + i);

		// for (int i = 0; i < listTenDichVu.size(); i++){
		String dichVu = null;
		
		if (i != -1) {	
			temp = i;
		}
		
		dichVu = listDichVuDuong.get(temp);
		System.out.println("Text: " + dichVu + " - " + temp);
		
//		for (int p = 0; p < listTenDichVu.size(); p++){
//		if (i != -1){
		
	/*	ArrayList<String> listDichVu = new ArrayList<String>();
		for (int j = 0; j < 6; j++){
			listDichVu.add(listDichVuDuong.get(temp)
					+ " " + listDuong.get(j));
		}
		list.add(listDichVu);
		}*/
		
		if (getStr().length() > dichVu.length() + 1 && i == -1) {
			String text = getStr().substring(dichVu.length() + 1,
					getStr().length()).trim();

			int[] array = searchArray(text, listDuong);
	//		int j = searchItem(text, listDuong);
			System.out.println("Text1: " + text + " - " + array[0]);
//			ArrayList<String> listDichVu = new ArrayList<String>();
	/*		if (j != -1){
				list.get(temp).clear();
//			for (int k = 0; k < j.length; k++) {
				list.get(temp).add(listDichVuDuong.get(temp)
						+ " " + listDuong.get(j));
				System.out.println("T: " + text + " - " + j);
	//		}
			}*/
			
			list.get(temp).clear();
			ArrayList<String> listDichVu = new ArrayList<String>();
			if (array != null) {
				for (int j = 0; j < array.length; j++) {
					if (array[j] != -1) {
			//			list.get(temp).trimToSize();
						list.get(temp).add(listDichVuDuong.get(temp) + " "
										+ listDuong.get(array[j]));
						System.out.println("T: " + text + " - " + j);
					}
				}
			}	
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				activity, R.layout.list_item,
				list.get(temp));

		System.out.println("Size: " + adapter.getCount());
		txtHomeSearch.setAdapter(adapter);
	}
	
	
	
	public int searchItem(String item, ArrayList<String> list){
		for (int i = 0;  i < list.size(); i++){
			if (true == list.get(i).contains(item)){
				return i;
			}
		}
		return -1;
	}
	
	public int[] searchArray(String item, ArrayList<String> list){
		int[] numbers = new int[6];
		int counter = 0;
		int i;
		for (i = 0;  i < list.size(); i++){
			if (true == list.get(i).contains(item)){
				numbers[counter] = i;
				counter++;
			}

			if (counter >= numbers.length){
				break;
			}
		}
		
		if (i == list.size() && counter < numbers.length){
			for (int j = counter;  j < numbers.length; j++){
				numbers[j] = -1;
			}
		}

		return numbers;
	}
	
}
